package com.example.nfcproducttracing;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;

public class ProductActivity extends Activity
{
	Product product = new Product();
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()))
		{
			
			ProductFragment productFragment = new ProductFragment();
			
			FragmentManager fm = getFragmentManager();
			FragmentTransaction ft = fm.beginTransaction();
			
			ft.replace(R.id.fragmentContainer, productFragment);
			ft.commit();
		}
	}
	
	public Product readNDEFRecord(Intent intent)
    {
    	if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
        {
            this.productToast();
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            if (rawMsgs != null)
            {
                NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
                for (int i = 0; i < rawMsgs.length; i++)
                {
                        msgs[i] = (NdefMessage) rawMsgs[i];
                }
                NdefRecord[] records = msgs[0].getRecords();
                for (int i = 0; i < records.length; i++)
                {
                    NdefRecord record = records[i];
                    String content = new String(record.getPayload());
                    if (content != null)
                    {
                    	String[] parts = content.split("&");
                        
                        if (parts.length > 12)
                        {
                            product.producer.name = parts[0];
                            product.producer.address = parts[1];
                            product.producer.postCode = parts[2];
                            product.producer.city = parts[3];
                            product.producer.country = parts[4];
                            product.producer.email = parts[5];
                            product.producer.telephone = parts[6];
                            
                            product.type = parts[7];
                            product.name = parts[8];
                            product.origin = parts[9];
                            product.productionDate = parts[10];
                            product.productionMethod = parts[11];
                            product.seed = parts[12];

                    		getIntent().setAction("");
                        }
                    }
                }
            }
        }
    	return product;
    }
	
	public void productToast()
	{
		Context context = getApplicationContext();
    	CharSequence text = "Product discovered.";
    	int duration = Toast.LENGTH_SHORT;

    	Toast toast = Toast.makeText(context, text, duration);
    	toast.show();
	}
	
}
